package com.study.springmvc.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.study.springmvc.dao.mapper.StudentMapper;
import com.study.springmvc.model.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.UUID;

@Controller
public class HelloWorldController {

    //@Autowired
    //private StudentService studentService;
    @Autowired
    StudentMapper studentMapper;

    @RequestMapping("/login")
    public ModelAndView login(@RequestParam("name")String name, @RequestParam("pwd") String pwd, ModelAndView modelAndView){

        Student student = studentMapper.findStudentByNameAndPwd(name, pwd);
        if (student != null) {
            return list(1, 5, modelAndView);
        }
        modelAndView.addObject("result", "用户名或密码错误");
        modelAndView.setViewName("login");
        return modelAndView;
    }

    @RequestMapping("/toUpdate")
    public ModelAndView toUpdate(@RequestParam("stuGid")String stuGid, ModelAndView modelAndView){
        Student student = studentMapper.findStudentByStuGid(stuGid);
        modelAndView.addObject("stu", student);
        modelAndView.setViewName("update");
        return modelAndView;
    }

    @RequestMapping("/updatePwd")
    public ModelAndView updatePwd(@RequestParam("stuGid")String stuGid
            , @RequestParam("pwd")String pwd, ModelAndView modelAndView) throws UnsupportedEncodingException {
        String newPwd = new String(pwd.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
        studentMapper.updatePwdByStuGid(newPwd, stuGid);
        modelAndView.addObject("result", "修改成功");
        modelAndView.setViewName("update");
        return modelAndView;
    }

    @RequestMapping("/delete")
    public ModelAndView updatePwd(@RequestParam("stuGid")String stuGid, ModelAndView modelAndView) {
        studentMapper.deleteByStuGid(stuGid);
        modelAndView.addObject("result", "删除成功");
        // 回到列表
        return list(1, 5, modelAndView);
    }

    @RequestMapping("/list")
    public ModelAndView list(@RequestParam("pageIndex")Integer pageIndex, @RequestParam("pageSize") Integer pageSize, ModelAndView modelAndView){
        // int studentCount = studentMapper.getStudentCount();
        // List<Student> studentList = studentMapper.findStudentList((pageIndex - 1) * pageSize, pageSize);


        Page<Student> page = PageHelper.startPage(pageIndex, pageSize, true);
        List<Student> studentList = studentMapper.findStudentListNoPageParam();
        modelAndView.addObject("studentList", studentList);
        modelAndView.addObject("studentCount", (int)page.getTotal());
        // 每页展示
        modelAndView.addObject("pageSize", pageSize);
        // 总记录数
        modelAndView.addObject("pageCount", page.getPages());
        // 当前页码
        modelAndView.addObject("pageIndex", pageIndex);
        modelAndView.setViewName("success");
        return modelAndView;
    }

    @RequestMapping("/toAdd")
    public ModelAndView add(ModelAndView modelAndView){
        modelAndView.setViewName("add");
        return modelAndView;
    }

    @RequestMapping("/add")
    public ModelAndView add(@RequestParam("name")String name, @RequestParam("pwd") String pwd, ModelAndView modelAndView){
        System.out.println(name + "\t" + pwd);
        Student student = new Student();
        int now = (int)(System.currentTimeMillis()/1000);
        student.setCreateTime(now);
        student.setUpdateTime(now);
        student.setStuGid(UUID.randomUUID().toString());
        student.setUserName(name);
        student.setPwd(pwd);
        int result = studentMapper.add(student);
        if (result > 0) {
            modelAndView.addObject("result", "添加成功");
            modelAndView.setViewName("success");
        } else {
            modelAndView.addObject("result", "添加失败");
            modelAndView.setViewName("success");
        }
        return modelAndView;
    }
}
